package com.coolgor.axnut.service.seal.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.coolgor.axnut.dao.seal.SealCategoryDao;
import com.coolgor.axnut.dto.seal.SealCategoryExecution;
import com.coolgor.axnut.entity.seal.SealCategory;
import com.coolgor.axnut.enums.seal.SealCategoryOperationStateEnum;
import com.coolgor.axnut.exceptions.seal.SealCategoryOperationException;
import com.coolgor.axnut.service.seal.SealCategoryService;
import com.coolgor.axnut.util.PageCalculator;


@Service
public class SealCategoryServiceImpl implements SealCategoryService {
	
	@Autowired
	private SealCategoryDao sealCategoryDao;
	
	
	@Override
	public SealCategory getSealCategory(Long sealCategoryID) {
		return sealCategoryDao.querySealCategoryByID(sealCategoryID);
	}

	
	@Override
	public SealCategoryExecution getSealCategoryList(SealCategory sealCategoryCondition, int pageIndex, int pageSize) {
		//将页码转换成行码
		int rowIndex = PageCalculator.calculateRowIndex(pageIndex, pageSize);
		//依据查询条件，调用dao层返回相关的查询结果列表
		List<SealCategory> list = sealCategoryDao.querySealCategoryList(sealCategoryCondition, rowIndex, pageSize);
		//依据相同的查询条件，返回查询结果总数
		int count = sealCategoryDao.querySealCategoryCount(sealCategoryCondition);
		SealCategoryExecution result = new SealCategoryExecution();
		if (list != null) {
			result.setState(SealCategoryOperationStateEnum.SUCCESS.getState());
			result.setStateInfo(SealCategoryOperationStateEnum.SUCCESS.getStateInfo());
			result.setList(list);
			result.setCount(count);
		} else {
			result.setState(SealCategoryOperationStateEnum.INNER_ERROR.getState());
		}
		return result;
	}
	
 
	@Override
	@Transactional
	public SealCategoryExecution addSealCategory(SealCategory sealCategory) {
		// 空值判断
		if (sealCategory == null) {
			return new SealCategoryExecution(SealCategoryOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 添加
			effectedNum = sealCategoryDao.insertSealCategory(sealCategory);
			if (effectedNum <= 0) {
				throw new SealCategoryOperationException("印章类型创建失败");
			} 
		} catch (Exception e) {
			throw new SealCategoryOperationException("add SealCategory Information error:" + e.getMessage());
		}
		return new SealCategoryExecution(SealCategoryOperationStateEnum.SUCCESS, sealCategory);
	}

	
	@Override
	@Transactional
	public SealCategoryExecution modifySealCategory(SealCategory sealCategory) {
		// 空值判断
		if (sealCategory == null) {
			return new SealCategoryExecution(SealCategoryOperationStateEnum.NULL);
		}
		int effectedNum;
		// 即将更新信息
		try {
			// 更新操作
			effectedNum = sealCategoryDao.updateSealCategory(sealCategory);
			if (effectedNum <= 0) {
				throw new SealCategoryOperationException("印章类型更新失败");
			} 
		} catch (Exception e) {
			throw new SealCategoryOperationException("Modify SealCategory Information error:" + e.getMessage());
		}
		return new SealCategoryExecution(SealCategoryOperationStateEnum.SUCCESS, sealCategory);
	}
	

	@Override
	@Transactional
	public SealCategoryExecution removeSealCategory(SealCategory sealCategory) {
		// 空值判断
		if (sealCategory == null) {
			return new SealCategoryExecution(SealCategoryOperationStateEnum.NULL);
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = sealCategoryDao.deleteSealCategory(sealCategory);
			if (effectedNum <= 0) {
				throw new SealCategoryOperationException("印章类型删除失败");
			} 
		} catch (Exception e) {
			throw new SealCategoryOperationException("Delete SealCategory Information error:" + e.getMessage());
		}
		return new SealCategoryExecution(SealCategoryOperationStateEnum.SUCCESS, sealCategory);
	}
	

	@Override
	@Transactional
	public SealCategoryExecution batchRemoveSealCategory(List<SealCategory> list) {
		// 空值判断
		if (list == null || list.size()<1) {
			return new SealCategoryExecution(SealCategoryOperationStateEnum.NULL);
		}
		List<Long> idList = new ArrayList<Long>();
		for (int i=0; i<list.size(); i++) {
			idList.add(list.get(i).getSealCategoryID());			
		}
		int effectedNum;
		try {
			// 执行删除操作
			effectedNum = sealCategoryDao.batchDeleteSealCategory(idList);
			if (effectedNum <= 0) {
				throw new SealCategoryOperationException("印章类型批量删除失败");
			} 
		} catch (Exception e) {
			throw new SealCategoryOperationException("Batch Delete SealCategory Information error:" + e.getMessage());
		}
		SealCategoryExecution exe = new SealCategoryExecution(SealCategoryOperationStateEnum.SUCCESS);
		//将删除的记录总数加入到返回值
		exe.setCount(effectedNum);
		return exe;
	}

}
